<?php

class OOWP_Atom_PostNav
    extends OOWP_Formattable
{

    public function __construct(stdClass $post, $query = null)
    {
        parent::__construct(array(
            'post' => $post,
            'query' => $query,
        ));
    }

    /* ACCESSORS */

    protected function getFormatPieces( )
    {
        $prev = '<span class="nav-previous">';
        $next = '<span class="nav-next">';

        if ($this->query instanceof WP_Query) {
            ob_start();
            previous_post_link( );
            $prev .= ob_get_contents();
            ob_end_clean();
            ob_start( );
            next_post_link( );
            $next .= ob_get_contents( );
            ob_end_clean( );
        } elseif ($this->query instanceof OOWP_Query) {
            $post_before = $this->query->find_post_before($this->post);
            if (!is_null($post_before)) {
                $prev_post = $post_before;
                $prev .= '<a href="' . get_permalink($prev_post->ID) . '"';
                $prev .= ' title="' . $prev_post->post_title . '">';
                $prev .= '&laquo; ' . $prev_post->post_title;
                $prev .= '</a>';
            }
            if (!is_null($this->query->find_post_after($this->post))) {
                $next_post = $this->query->find_post_after($this->post);
                $next .= '<a href="' . get_permalink($next_post->ID) . '"';
                $next .= ' title="' . $next_post->post_title . '">';
                $next .= $next_post->post_title . ' &raquo;';
                $next .= '</a>';
            }
        }

        $prev .= '</span>';
        $next .= '</span>';

        return array($prev, $next);
    }

    protected function getPost( )
    {
        return $this->dict['post'];
    }

    protected function setPost(stdClass $post)
    {
        $this->dict['post'] = $post;
    }

    protected function getQuery( )
    {
        return $this->dict['query'];
    }

    protected function setQuery($query)
    {
        $this->dict['query'] = $query;
    }

}
